package com.wyp.didishige.activity.users;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.wyp.didishige.R;
import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.adapter.BlackListAdapter;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.db.BlackUser;
import com.wyp.didishige.db.BlackUserManager;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.wiget.dialog.DialogTips;

/**
 * 黑名单列表
 */
public class BlackListActivity extends BaseActivity implements OnItemClickListener {

	ListView listview;
	BlackListAdapter adapter;
    private TextView noData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_blacklist);
		actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("黑名单");
		initView();
	}

	private void initView() {
		adapter = new BlackListAdapter(this, BlackUserManager.getInstance(this).getAllBlackUser());
		listview = (ListView) findViewById(R.id.list_blacklist);
        assert listview != null;
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                BlackUser invite = (BlackUser) adapter.getItem(position);
                showRemoveBlackDialog(position,invite);
                return true;
            }
        });
		listview.setAdapter(adapter);

        noData = (TextView) findViewById(R.id.noData);
	}

	/** 显示移除黑名单对话框
	  */
	public void showRemoveBlackDialog(final int position, final BlackUser user) {
		DialogTips dialog = new DialogTips(this, "移出黑名单",
				"你确定将"+user.getName()+"移出黑名单吗?", "确定", true, true);
		// 设置成功事件
		dialog.SetOnSuccessListener(new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialogInterface, int userId) {
				adapter.remove(position);
                BlackUserManager.getInstance(BlackListActivity.this).deleteBlackUser(user);
                ShowToast("移出黑名单成功");
                if (adapter.getCount()==0){
                    noData.setText("暂无黑名单用户");
                    noData.setVisibility(View.VISIBLE);
                }else{
                    noData.setVisibility(View.GONE);
                }
			}
		});
		// 显示确认对话框
		dialog.show();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		BlackUser blackUser = (BlackUser) adapter.getItem(arg2);
        Bundle bundle=new Bundle();
        bundle.putString(Config.KEY_USER_ID,blackUser.getUid());
        IntentUtil.start_activity(this,UserInfoActivity.class,bundle);
	}

    @Override
    public void onResume() {
        super.onResume();
        adapter.setList(BlackUserManager.getInstance(this).getAllBlackUser());
        adapter.notifyDataSetChanged();
        if (adapter.getCount()==0){
            noData.setText("暂无黑名单用户");
            noData.setVisibility(View.VISIBLE);
        }else{
            noData.setVisibility(View.GONE);
        }
    }
}
